package ca.unbc.cpsc672.fuzzy.function;

public class PiecewiseLast extends MembershipFunction {
	private static final String P1 = "P1";
	private static final String P2 = "P2";

	public PiecewiseLast(String label, double[] paras) {
		super(label);
		addParamenter(P1, paras[0]);
		addParamenter(P2, paras[1]);
	}

	@Override
	public double getCenter() {
		return (getParamenter(P2) - getParamenter(P1)) / 2 + getParamenter(P2);
	}

	// LaTeX code
	// f(x)=\begin{cases}
	// & 0\text{ if } x < p_{}1\\
	// & mx+n\text{ if } p1 \leqslant x < p_{}2 \\
	// & 1\text{ if } x \geqslant p_{}2
	// \end{cases}
	@Override
	protected String getImageName() {
		return "PiecewiseLast.gif";
	}

	@Override
	protected double calcFunction(double x) {
		double p1 = getParamenter(P1);
		double p2 = getParamenter(P2);
		if (x > p1 && x < p2) {
			double m = 1 / (p2 - p1);
			double n = -m * p1;
			return m * x + n;
		} else if (x >= p2) {
			return 1;
		} else
			return 0;
	}
}
